package fr.esiag.isidis.fi.pdsTeam.tram;

public class TramGenerator {
	static TramGenerator instance=null;
	
	private TramGenerator() {
	}
	
	public static TramGenerator getInstance() {
		if(instance==null) instance=new TramGenerator();
		return instance;
	}
	
	/*private static byte addByte(int bit,byte x)
	{
		 x=(byte)(x<<1);
		 x= (byte)(x | bit);
		 return x;
	}
		
	private static byte setInByte(int val1,int val2){
		byte b=(byte)val1;
		int n2=Integer.toBinaryString(val2).length();
		b=(byte)(b<<4-n2);
		String s=Integer.toBinaryString(val2);
		for (int i = 0; i < s.length(); i++) {
			if(s.charAt(i)=='1')
				b=addByte(1, b);
			else
				b=addByte(0, b);
		}
		return b;
	}
	
	
	public static byte[] generateTram (int TypeSource,int idSource,int idStation,int codeAlerte){
		byte b1=setInByte(TypeSource, idSource);
		byte b2=setInByte(idStation, codeAlerte);
		byte[]t=new byte[]{b1,b2};
		
		return t;
	}*/
	
	public static byte[] generateTram (String sensorMacAddress,int codeAlerte){
		byte[]mac = sensorMacAddress.getBytes();
		
		byte[] tram = new byte[mac.length+2];
		tram[0] = (byte)codeAlerte;
		tram[1] = (byte)sensorMacAddress.length();
		for (int i = 0; i < mac.length; i++) {
			tram[i+2] = mac[i];
		}
		return tram;
	}
	
	public static byte[] extractTram(byte[] tram){
			
			int codeAlerte = new Integer(tram[0]);
			int macLength = new Integer(tram[1]);
			
			System.out.println("code:   "+codeAlerte);
			System.out.println("length: "+macLength);
			
			byte[]macByteArray = new byte[macLength];
			
			for (int i = 2; i < macLength+2; i++) {
				macByteArray[i-2] = tram[i];
			}
			
			System.out.println("chaine extraite: "+new String(macByteArray));
			return tram;
		}
	
}
